﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Terraria.Graphics
{
	// Token: 0x02000384 RID: 900
	public class SpriteViewMatrix
	{
		// Token: 0x17000223 RID: 547
		// (get) Token: 0x06001FDC RID: 8156 RVA: 0x000164DF File Offset: 0x000146DF
		// (set) Token: 0x06001FDD RID: 8157 RVA: 0x000164E7 File Offset: 0x000146E7
		public Vector2 Zoom
		{
			get
			{
				return this._zoom;
			}
			set
			{
				if (this._zoom != value)
				{
					this._zoom = value;
					this._needsRebuild = true;
				}
			}
		}

		// Token: 0x17000224 RID: 548
		// (get) Token: 0x06001FDE RID: 8158 RVA: 0x00016505 File Offset: 0x00014705
		public Vector2 Translation
		{
			get
			{
				if (this.ShouldRebuild())
				{
					this.Rebuild();
				}
				return this._translation;
			}
		}

		// Token: 0x17000225 RID: 549
		// (get) Token: 0x06001FDF RID: 8159 RVA: 0x0001651B File Offset: 0x0001471B
		public Matrix ZoomMatrix
		{
			get
			{
				if (this.ShouldRebuild())
				{
					this.Rebuild();
				}
				return this._zoomMatrix;
			}
		}

		// Token: 0x17000226 RID: 550
		// (get) Token: 0x06001FE0 RID: 8160 RVA: 0x00016531 File Offset: 0x00014731
		public Matrix TransformationMatrix
		{
			get
			{
				if (this.ShouldRebuild())
				{
					this.Rebuild();
				}
				return this._transformationMatrix;
			}
		}

		// Token: 0x17000227 RID: 551
		// (get) Token: 0x06001FE1 RID: 8161 RVA: 0x00016547 File Offset: 0x00014747
		// (set) Token: 0x06001FE2 RID: 8162 RVA: 0x0001654F File Offset: 0x0001474F
		public SpriteEffects Effects
		{
			get
			{
				return this._effects;
			}
			set
			{
				if (this._effects != value)
				{
					this._effects = value;
					this._needsRebuild = true;
				}
			}
		}

		// Token: 0x17000228 RID: 552
		// (get) Token: 0x06001FE3 RID: 8163 RVA: 0x00016568 File Offset: 0x00014768
		public Matrix EffectMatrix
		{
			get
			{
				if (this.ShouldRebuild())
				{
					this.Rebuild();
				}
				return this._effectMatrix;
			}
		}

		// Token: 0x06001FE4 RID: 8164 RVA: 0x0045A578 File Offset: 0x00458778
		public SpriteViewMatrix(GraphicsDevice graphicsDevice)
		{
			this._graphicsDevice = graphicsDevice;
		}

		// Token: 0x06001FE5 RID: 8165 RVA: 0x0045A5C8 File Offset: 0x004587C8
		private void Rebuild()
		{
			if (!this._overrideSystemViewport)
			{
				this._viewport = this._graphicsDevice.Viewport;
			}
			Vector2 vector = new Vector2((float)this._viewport.Width, (float)this._viewport.Height);
			Matrix matrix = Matrix.Identity;
			if (this._effects.HasFlag(SpriteEffects.FlipHorizontally))
			{
				matrix *= Matrix.CreateScale(-1f, 1f, 1f) * Matrix.CreateTranslation(vector.X, 0f, 0f);
			}
			if (this._effects.HasFlag(SpriteEffects.FlipVertically))
			{
				matrix *= Matrix.CreateScale(1f, -1f, 1f) * Matrix.CreateTranslation(0f, vector.Y, 0f);
			}
			Vector2 value = vector * 0.5f;
			Vector2 vector2 = value - value / this._zoom;
			this._translation = vector2;
			this._zoomMatrix = Matrix.CreateTranslation(-vector2.X, -vector2.Y, 0f) * Matrix.CreateScale(this._zoom.X, this._zoom.Y, 1f);
			this._effectMatrix = matrix;
			this._transformationMatrix = matrix * this._zoomMatrix;
			this._needsRebuild = false;
		}

		// Token: 0x06001FE6 RID: 8166 RVA: 0x0001657E File Offset: 0x0001477E
		public void SetViewportOverride(Viewport viewport)
		{
			this._viewport = viewport;
			this._overrideSystemViewport = true;
		}

		// Token: 0x06001FE7 RID: 8167 RVA: 0x0001658E File Offset: 0x0001478E
		public void ClearViewportOverride()
		{
			this._overrideSystemViewport = false;
		}

		// Token: 0x06001FE8 RID: 8168 RVA: 0x0045A738 File Offset: 0x00458938
		private bool ShouldRebuild()
		{
			return this._needsRebuild || (!this._overrideSystemViewport && (this._graphicsDevice.Viewport.Width != this._viewport.Width || this._graphicsDevice.Viewport.Height != this._viewport.Height));
		}

		// Token: 0x04003CA8 RID: 15528
		private Vector2 _zoom = Vector2.One;

		// Token: 0x04003CA9 RID: 15529
		private Vector2 _translation = Vector2.Zero;

		// Token: 0x04003CAA RID: 15530
		private Matrix _zoomMatrix = Matrix.Identity;

		// Token: 0x04003CAB RID: 15531
		private Matrix _transformationMatrix = Matrix.Identity;

		// Token: 0x04003CAC RID: 15532
		private SpriteEffects _effects;

		// Token: 0x04003CAD RID: 15533
		private Matrix _effectMatrix;

		// Token: 0x04003CAE RID: 15534
		private GraphicsDevice _graphicsDevice;

		// Token: 0x04003CAF RID: 15535
		private Viewport _viewport;

		// Token: 0x04003CB0 RID: 15536
		private bool _overrideSystemViewport;

		// Token: 0x04003CB1 RID: 15537
		private bool _needsRebuild = true;
	}
}
